using System;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
using OpenNETCF.Win32;

namespace PPCHelpReader
{
	/// <summary>
	/// Summary description for Options.
	/// </summary>
	public class Options : System.Windows.Forms.Form
	{
		private System.Windows.Forms.CheckBox cbAllowCHB;
        private System.Windows.Forms.CheckBox cbWipeDir;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox txtTempDir;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ComboBox cmbSearchIndex;
		private System.Windows.Forms.CheckBox cbRemoveScript;
		private System.Windows.Forms.CheckBox cbRemoveXML;
		private System.Windows.Forms.CheckBox cbRemoveDisplayNone;
		private System.Windows.Forms.CheckBox cbHTTPDecode;
        private TabControl tabControl1;
        private TabPage tpGeneral;
        private TabPage tpHTML;
        private Label label1;
        private Label label3;
        private Label label5;
        private Label label6;
        private Label label8;
        private Label label7;
        private CheckBox cbIgnoreImages;
        private TabPage tpFiles;

        /// <summary>
        /// Initializes a new instance of the <see cref="Options"/> class.
        /// </summary>
		public Options()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		
			// Read settings from registry
			RegistryKey RegKey=null;
			try
			{				
				RegKey=OpenNETCF.Win32.Registry.CurrentUser.OpenSubKey(@"Software\PPCHelpReader.NET");
				if (RegKey!=null)
				{
					int iInt=Convert.ToInt32(RegKey.GetValue("AllowCHB",1));
					if (iInt!=0)
						cbAllowCHB.Checked=true;
					else
						cbAllowCHB.Checked=false;	
		
					iInt=Convert.ToInt32(RegKey.GetValue("WipeTempDir",0));
					if (iInt!=0)
						cbWipeDir.Checked=true;
					else
						cbWipeDir.Checked=false;	
					
					iInt=Convert.ToInt32(RegKey.GetValue("CreateSearch",0));
					this.cmbSearchIndex.SelectedIndex=iInt;

					txtTempDir.Text=Convert.ToString(RegKey.GetValue("TempDir",@"\Temp"));
					if (txtTempDir.Text=="")
						txtTempDir.Text=@"\Temp";

					iInt=Convert.ToInt32(RegKey.GetValue("RemoveScriptTags",1));
					if (iInt!=0) cbRemoveScript.Checked=true;
					else cbRemoveScript.Checked=false;	

					iInt=Convert.ToInt32(RegKey.GetValue("RemoveXMLTags",1));
					if (iInt!=0) cbRemoveXML.Checked=true;
					else cbRemoveXML.Checked=false;	

					iInt=Convert.ToInt32(RegKey.GetValue("RemoveDisplayNone",1));
					if (iInt!=0) cbRemoveDisplayNone.Checked=true;
					else cbRemoveDisplayNone.Checked=false;	

					iInt=Convert.ToInt32(RegKey.GetValue("UseHTTPDecode",1));
					if (iInt!=0) cbHTTPDecode.Checked=true;
					else cbHTTPDecode.Checked=false;

                    iInt = Convert.ToInt32(RegKey.GetValue("IgnoreImages", 0));
                    if (iInt != 0) cbIgnoreImages.Checked = true;
                    else cbIgnoreImages.Checked = false;
				}
				else
				{
					cbAllowCHB.Checked=true;
					cbWipeDir.Checked=false;
					cmbSearchIndex.SelectedIndex=1;
					txtTempDir.Text=@"\Temp";
					cbRemoveScript.Checked=false;
					cbRemoveXML.Checked=false;
					cbRemoveDisplayNone.Checked=true;
					cbHTTPDecode.Checked=false;
                    cbIgnoreImages.Checked = false;
				}
			}
			finally
			{
				if (RegKey!=null)
					RegKey.Close();
			}			
			cbAllowCHB_CheckStateChanged(this,EventArgs.Empty);
			if (cmbSearchIndex.SelectedIndex==-1)
				cmbSearchIndex.SelectedIndex=1;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.cbAllowCHB = new System.Windows.Forms.CheckBox();
            this.cbWipeDir = new System.Windows.Forms.CheckBox();
            this.label4 = new System.Windows.Forms.Label();
            this.txtTempDir = new System.Windows.Forms.TextBox();
            this.cmbSearchIndex = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.cbRemoveScript = new System.Windows.Forms.CheckBox();
            this.cbRemoveXML = new System.Windows.Forms.CheckBox();
            this.cbRemoveDisplayNone = new System.Windows.Forms.CheckBox();
            this.cbHTTPDecode = new System.Windows.Forms.CheckBox();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tpGeneral = new System.Windows.Forms.TabPage();
            this.label1 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.tpHTML = new System.Windows.Forms.TabPage();
            this.cbIgnoreImages = new System.Windows.Forms.CheckBox();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.tpFiles = new System.Windows.Forms.TabPage();
            this.label8 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.tabControl1.SuspendLayout();
            this.tpGeneral.SuspendLayout();
            this.tpHTML.SuspendLayout();
            this.tpFiles.SuspendLayout();
            this.SuspendLayout();
            // 
            // cbAllowCHB
            // 
            this.cbAllowCHB.Dock = System.Windows.Forms.DockStyle.Top;
            this.cbAllowCHB.Location = new System.Drawing.Point(10, 0);
            this.cbAllowCHB.Name = "cbAllowCHB";
            this.cbAllowCHB.Size = new System.Drawing.Size(220, 41);
            this.cbAllowCHB.TabIndex = 11;
            this.cbAllowCHB.Text = "Allow Creation of QuickLoad Files";
            this.cbAllowCHB.CheckStateChanged += new System.EventHandler(this.cbAllowCHB_CheckStateChanged);
            // 
            // cbWipeDir
            // 
            this.cbWipeDir.Dock = System.Windows.Forms.DockStyle.Top;
            this.cbWipeDir.Location = new System.Drawing.Point(10, 0);
            this.cbWipeDir.Name = "cbWipeDir";
            this.cbWipeDir.Size = new System.Drawing.Size(212, 43);
            this.cbWipeDir.TabIndex = 9;
            this.cbWipeDir.Text = "Wipe Temp Dir (Slower, Use less Kb)";
            // 
            // label4
            // 
            this.label4.Dock = System.Windows.Forms.DockStyle.Top;
            this.label4.Location = new System.Drawing.Point(10, 43);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(212, 34);
            this.label4.Text = "Temp Directory";
            // 
            // txtTempDir
            // 
            this.txtTempDir.Dock = System.Windows.Forms.DockStyle.Top;
            this.txtTempDir.Location = new System.Drawing.Point(10, 77);
            this.txtTempDir.Name = "txtTempDir";
            this.txtTempDir.ReadOnly = true;
            this.txtTempDir.Size = new System.Drawing.Size(212, 21);
            this.txtTempDir.TabIndex = 6;
            // 
            // cmbSearchIndex
            // 
            this.cmbSearchIndex.Dock = System.Windows.Forms.DockStyle.Top;
            this.cmbSearchIndex.Items.Add("Create On first use");
            this.cmbSearchIndex.Items.Add("Create On first load");
            this.cmbSearchIndex.Location = new System.Drawing.Point(10, 77);
            this.cmbSearchIndex.Name = "cmbSearchIndex";
            this.cmbSearchIndex.Size = new System.Drawing.Size(220, 22);
            this.cmbSearchIndex.TabIndex = 5;
            // 
            // label2
            // 
            this.label2.Dock = System.Windows.Forms.DockStyle.Top;
            this.label2.Location = new System.Drawing.Point(10, 41);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(220, 36);
            this.label2.Text = "Search Index Creation";
            // 
            // cbRemoveScript
            // 
            this.cbRemoveScript.Dock = System.Windows.Forms.DockStyle.Top;
            this.cbRemoveScript.Location = new System.Drawing.Point(10, 40);
            this.cbRemoveScript.Name = "cbRemoveScript";
            this.cbRemoveScript.Size = new System.Drawing.Size(220, 40);
            this.cbRemoveScript.TabIndex = 3;
            this.cbRemoveScript.Text = "Remove Script Tags";
            // 
            // cbRemoveXML
            // 
            this.cbRemoveXML.Dock = System.Windows.Forms.DockStyle.Top;
            this.cbRemoveXML.Location = new System.Drawing.Point(10, 0);
            this.cbRemoveXML.Name = "cbRemoveXML";
            this.cbRemoveXML.Size = new System.Drawing.Size(220, 40);
            this.cbRemoveXML.TabIndex = 2;
            this.cbRemoveXML.Text = "Remove XML Tags";
            // 
            // cbRemoveDisplayNone
            // 
            this.cbRemoveDisplayNone.Dock = System.Windows.Forms.DockStyle.Top;
            this.cbRemoveDisplayNone.Location = new System.Drawing.Point(10, 120);
            this.cbRemoveDisplayNone.Name = "cbRemoveDisplayNone";
            this.cbRemoveDisplayNone.Size = new System.Drawing.Size(220, 39);
            this.cbRemoveDisplayNone.TabIndex = 1;
            this.cbRemoveDisplayNone.Text = "Remove Display None";
            // 
            // cbHTTPDecode
            // 
            this.cbHTTPDecode.Dock = System.Windows.Forms.DockStyle.Top;
            this.cbHTTPDecode.Location = new System.Drawing.Point(10, 80);
            this.cbHTTPDecode.Name = "cbHTTPDecode";
            this.cbHTTPDecode.Size = new System.Drawing.Size(220, 40);
            this.cbHTTPDecode.TabIndex = 0;
            this.cbHTTPDecode.Text = "Use HTTP Decode";
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tpGeneral);
            this.tabControl1.Controls.Add(this.tpHTML);
            this.tabControl1.Controls.Add(this.tpFiles);
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Location = new System.Drawing.Point(0, 0);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(240, 294);
            this.tabControl1.TabIndex = 12;
            // 
            // tpGeneral
            // 
            this.tpGeneral.Controls.Add(this.cmbSearchIndex);
            this.tpGeneral.Controls.Add(this.label2);
            this.tpGeneral.Controls.Add(this.cbAllowCHB);
            this.tpGeneral.Controls.Add(this.label1);
            this.tpGeneral.Controls.Add(this.label3);
            this.tpGeneral.Location = new System.Drawing.Point(0, 0);
            this.tpGeneral.Name = "tpGeneral";
            this.tpGeneral.Size = new System.Drawing.Size(240, 271);
            this.tpGeneral.Text = "General";
            // 
            // label1
            // 
            this.label1.Dock = System.Windows.Forms.DockStyle.Left;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(10, 271);
            // 
            // label3
            // 
            this.label3.Dock = System.Windows.Forms.DockStyle.Right;
            this.label3.Location = new System.Drawing.Point(230, 0);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(10, 271);
            // 
            // tpHTML
            // 
            this.tpHTML.Controls.Add(this.cbIgnoreImages);
            this.tpHTML.Controls.Add(this.cbRemoveDisplayNone);
            this.tpHTML.Controls.Add(this.cbHTTPDecode);
            this.tpHTML.Controls.Add(this.cbRemoveScript);
            this.tpHTML.Controls.Add(this.cbRemoveXML);
            this.tpHTML.Controls.Add(this.label5);
            this.tpHTML.Controls.Add(this.label6);
            this.tpHTML.Location = new System.Drawing.Point(0, 0);
            this.tpHTML.Name = "tpHTML";
            this.tpHTML.Size = new System.Drawing.Size(240, 271);
            this.tpHTML.Text = "HTML";
            // 
            // cbIgnoreImages
            // 
            this.cbIgnoreImages.Dock = System.Windows.Forms.DockStyle.Top;
            this.cbIgnoreImages.Location = new System.Drawing.Point(10, 159);
            this.cbIgnoreImages.Name = "cbIgnoreImages";
            this.cbIgnoreImages.Size = new System.Drawing.Size(220, 39);
            this.cbIgnoreImages.TabIndex = 6;
            this.cbIgnoreImages.Text = "Ignore Images";
            // 
            // label5
            // 
            this.label5.Dock = System.Windows.Forms.DockStyle.Right;
            this.label5.Location = new System.Drawing.Point(230, 0);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(10, 271);
            // 
            // label6
            // 
            this.label6.Dock = System.Windows.Forms.DockStyle.Left;
            this.label6.Location = new System.Drawing.Point(0, 0);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(10, 271);
            // 
            // tpFiles
            // 
            this.tpFiles.Controls.Add(this.txtTempDir);
            this.tpFiles.Controls.Add(this.label4);
            this.tpFiles.Controls.Add(this.cbWipeDir);
            this.tpFiles.Controls.Add(this.label8);
            this.tpFiles.Controls.Add(this.label7);
            this.tpFiles.Location = new System.Drawing.Point(0, 0);
            this.tpFiles.Name = "tpFiles";
            this.tpFiles.Size = new System.Drawing.Size(232, 268);
            this.tpFiles.Text = "Files";
            // 
            // label8
            // 
            this.label8.Dock = System.Windows.Forms.DockStyle.Left;
            this.label8.Location = new System.Drawing.Point(0, 0);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(10, 268);
            // 
            // label7
            // 
            this.label7.Dock = System.Windows.Forms.DockStyle.Right;
            this.label7.Location = new System.Drawing.Point(222, 0);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(10, 268);
            // 
            // Options
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(240, 294);
            this.Controls.Add(this.tabControl1);
            this.Name = "Options";
            this.Text = "Options";
            this.Closing += new System.ComponentModel.CancelEventHandler(this.Options_Closing);
            this.tabControl1.ResumeLayout(false);
            this.tpGeneral.ResumeLayout(false);
            this.tpHTML.ResumeLayout(false);
            this.tpFiles.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion		

		private void SaveOptions()
		{
			RegistryKey RegKey=null;
			try
			{
				RegKey=Registry.CurrentUser.CreateSubKey(@"Software\PPCHelpReader.NET");
				RegKey.SetValue("AllowCHB",Convert.ToInt32(cbAllowCHB.Checked));
				RegKey.SetValue("WipeTempDir",Convert.ToInt32(cbWipeDir.Checked));
				RegKey.SetValue("TempDir",this.txtTempDir.Text);
				RegKey.SetValue("CreateSearch",Convert.ToInt32(cmbSearchIndex.SelectedIndex));
				RegKey.SetValue("RemoveScriptTags",Convert.ToInt32(cbRemoveScript.Checked));
				RegKey.SetValue("RemoveXMLTags",Convert.ToInt32(cbRemoveXML.Checked));
				RegKey.SetValue("RemoveDisplayNone",Convert.ToInt32(cbRemoveDisplayNone.Checked));
				RegKey.SetValue("UseHTTPDecode",Convert.ToInt32(cbHTTPDecode.Checked));
                RegKey.SetValue("IgnoreImages", Convert.ToInt32(cbIgnoreImages.Checked));
			}
			finally
			{
				if (RegKey!=null)
					RegKey.Close();
			}
		}
		
		private void btnBrowse_Click(object sender, System.EventArgs e)
		{			
			FileSelectDlg Dlg=new FileSelectDlg(true);
			if (Dlg.ShowDialog()==DialogResult.OK)			
			{
				txtTempDir.Text=Dlg.FileName;
			}
		}

		private void Options_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			SaveOptions();
		}

		private void cbAllowCHB_CheckStateChanged(object sender, System.EventArgs e)
		{
			cmbSearchIndex.Enabled=this.cbAllowCHB.Checked;
        }        
	}
}
